package com.hll.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hll.dao.BaseDao;
import com.hll.service.BaseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by CGL on 2017-06-24 14:18:46.
 */
public abstract class BaseServiceImpl<T, Q, P> implements BaseService<T, Q, P> {
    protected BaseDao<T, Q> baseDao;
    private Class<?> type = null;

    public abstract void setDao();

    public BaseServiceImpl() {
        if(this.type == null && !this.getClass().getName().contains("EnhancerBySpringCGLIB") && this.getClass().getGenericSuperclass() instanceof ParameterizedType) {
            ParameterizedType superGclass = (ParameterizedType)this.getClass().getGenericSuperclass();
            Type[] arguments = superGclass.getActualTypeArguments();
            if(arguments.length > 0) {
                this.type = (Class)arguments[0];
            }
        }

    }

    public T getById(@PathVariable("id") P id) {
        return this.selectByPrimaryKey(id);
    }

    public List<T> getByIds(@RequestBody List<P> ids) {
        return this.selectByPrimaryKeys(ids);
    }

    public T selectByPrimaryKey(@PathVariable("id") P id) {
        this.setDao();
        return this.baseDao.selectByPrimaryKey(id);
    }

    public List<T> selectByPrimaryKeys(@RequestBody List<P> ids) {
        ArrayList list = new ArrayList();
        Iterator var4 = ids.iterator();

        while(var4.hasNext()) {
            Object id = (Object)var4.next();
            list.add(this.selectByPrimaryKey((P)id));
        }

        return list;
    }

    public List<T> selectByExample(@RequestBody Q example) {
        this.setDao();
        return this.baseDao.selectByExample(example);
    }

    public List<T> selectByExampleWithBLOBs(@RequestBody Q example) {
        this.setDao();
        return this.baseDao.selectByExampleWithBLOBs(example);
    }

    @Transactional
    public Object insert(@RequestBody T t) {
        this.setDao();
        return Integer.valueOf(this.baseDao.insert(t));
    }

    @Transactional
    public Object insertSelective(@RequestBody T t) {
        this.setDao();
        return Integer.valueOf(this.baseDao.insertSelective(t));
    }

    @Transactional
    public void insertBatch(@RequestBody List<T> models) {
        Iterator var3 = models.iterator();

        while(var3.hasNext()) {
            Object t = (Object)var3.next();
            this.insert((T)t);
        }

    }

    @Transactional
    public void insertSelectiveBatch(@RequestBody List<T> models) {
        Iterator var3 = models.iterator();

        while(var3.hasNext()) {
            T t = (T)var3.next();
            this.insertSelective(t);
        }

    }

    @Transactional
    public Object insertByJSON(String t) {
        return this.insert((T)JSON.parseObject(t, this.type));
    }

    @Transactional
    public Object insertByJSONObject(JSONObject t) {
        return this.insert((T)JSON.toJavaObject(t, this.type));
    }

    @Transactional
    public Object insertSelectiveByJSON(String t) {
        return this.insertSelective((T)JSON.parseObject(t, this.type));
    }

    @Transactional
    public Object insertSelectiveByJSONObject(JSONObject t) {
        return this.insertSelective((T)JSON.toJavaObject(t, this.type));
    }

    @Transactional
    public int updateByPrimaryKeySelective(@RequestBody T t) {
        this.setDao();
        return this.baseDao.updateByPrimaryKeySelective(t);
    }

    @Transactional
    public int updateByJSONPrimaryKey(String t) {
        this.setDao();
        return this.updateByPrimaryKey((T)JSON.parseObject(t, this.type));
    }

    @Transactional
    public int updateByJSONPrimaryKeySelective(String t) {
        this.setDao();
        return this.updateByPrimaryKeySelective((T)JSON.parseObject(t, this.type));
    }

    @Transactional
    public int updateByJSONObjectPrimaryKey(JSONObject t) {
        this.setDao();
        return this.updateByPrimaryKey((T)JSON.toJavaObject(t, this.type));
    }

    @Transactional
    public int updateByJSONObjectPrimaryKeySelective(JSONObject t) {
        this.setDao();
        return this.updateByPrimaryKeySelective((T)JSON.toJavaObject(t, this.type));
    }

    @Transactional
    public int updateByExampleSelective(T t, Q q) {
        this.setDao();
        return this.baseDao.updateByExampleSelective(t, q);
    }

    @Transactional
    public int updateByExampleWithBLOBs(T t, Q q) {
        this.setDao();
        return this.baseDao.updateByExampleWithBLOBs(t, q);
    }

    @Transactional
    public int updateByExample(T t, Q q) {
        this.setDao();
        return this.baseDao.updateByExample(t, q);
    }

    @Transactional
    public int updateByPrimaryKey(T t) {
        this.setDao();
        return this.baseDao.updateByPrimaryKey(t);
    }

    public int countByExample(@RequestBody Q example) {
        this.setDao();
        return this.baseDao.countByExample(example);
    }

    @Transactional
    public int deleteByPrimaryKey(@PathVariable("id") P id) {
        this.setDao();
        return this.baseDao.deleteByPrimaryKey(id);
    }

    @Transactional
    public int deleteByExample(@RequestBody Q q) {
        this.setDao();
        return this.baseDao.deleteByExample(q);
    }
}
